package dataSystem;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import com.opencsv.CSVWriter;

public class DataExporter {
    public static void exportToCsv(ReportGenerator.InventoryReport report, String filename) 
        throws IOException {
        try (CSVWriter writer = new CSVWriter(new FileWriter(filename))) {
            writer.writeNext(new String[]{"指标", "值"});
            writer.writeNext(new String[]{"库存周转率", String.format("%.2f", report.getTurnoverRate())});
            writer.writeNext(new String[]{"总销售量", String.valueOf(report.getSalesVolume())});
            writer.writeNext(new String[]{"平均库存量", String.format("%.2f", report.getAverageInventory())});
        }
    }
    
    public static void exportSupplierReportToCsv(ReportGenerator.SupplierReport report, String filename) 
        throws IOException {
        try (CSVWriter writer = new CSVWriter(new FileWriter(filename))) {
            writer.writeNext(new String[]{"供应商ID", "总订单数", "准时交付数", "质量问题数", "绩效评分"});
            
            for (Map.Entry<String, ReportGenerator.SupplierPerformance> entry : 
                report.getSupplierPerformance().entrySet()) {
                ReportGenerator.SupplierPerformance sp = entry.getValue();
                writer.writeNext(new String[]{
                    sp.getSupplierId(),
                    String.valueOf(sp.getTotalOrders()),
                    String.valueOf(sp.getOnTimeDeliveries()),
                    String.valueOf(sp.getQualityIssues()),
                    String.format("%.2f", sp.getPerformanceScore())
                });
            }
        }
    }
}